/*
 * Copyright (c) 2001, 2002 Boris Pruessmann and others. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Common Public License v0.5 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.core.resources;

import com.google.common.collect.Lists;

import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.PerforceException;
import net.sourceforge.perforce.core.PerforceSynchronizer;
import net.sourceforge.perforce.core.api.IPerforceServer;
import net.sourceforge.perforce.core.syncinfo.FolderSyncInfo;
import net.sourceforge.perforce.core.syncinfo.ResourceSyncInfo;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;

import java.util.List;

/**
 * @version $Revision: 488 $
 * @author <a href="mailto:bpruessmann@users.sourceforge.net">Boris
 *         Pruessmann</a>
 */
public class LocalFolder extends LocalResource {
  // --------------------------------------------------------------- Public
  // Static Factory Methods

  /**
   * Call this method to create a LocalFolder instance.
   * 
   * @param server the perforce server.
   * @param resource the local resource.
   * @return the LocalFolder instance.
   */
  public static final LocalFolder create(IPerforceServer server, IResource resource) {
    return new LocalFolder(server, resource);
  }

  //----------------------------------------------------------------------------
  // ---- Constructors

  /**
   * Constructor for LocalFolder.
   * 
   * @see LocalResource#LocalResource(IPerforceServer, IResource)
   */
  private LocalFolder(IPerforceServer server, IResource resource) {
    super(server, resource);
  }

  //----------------------------------------------------------------------------
  // -- Public Methods

  /**
   * Checks whether the folder exists on the server.
   * 
   * @return true if the folder exists.
   */


  @Override
  public boolean hasRemote() {
    try {
      IContainer container = (IContainer) toResource();
      if (container == null) {
        return false;
      }

      FolderSyncInfo syncInfo = PerforceCorePlugin.getSynchronizer().getFolderSync(container);

      return syncInfo != null;
    } catch (PerforceException e) {
      PerforceCorePlugin.log(e.getStatus());
      return false;
    }
  }

  /**
   * Returns the contained files.
   * 
   * @return array of LocalFile.
   * 
   * @throws PerforceException in case of an error.
   */
  public LocalFile[] getFiles() throws PerforceException {
    try {
      IContainer container = (IContainer) toResource();
      IResource[] members = container.members();

      List<LocalFile> result = Lists.newArrayList();
      for (int i = 0, length = members.length; i < length; i++) {
        if (members[i].getType() == IResource.FILE) {
          result.add(LocalFile.create(getServer(), members[i]));
        }
      }

      return result.toArray(new LocalFile[0]);
    } catch (CoreException e) {
      throw new PerforceException(e.getStatus());
    }
  }

  /**
   * Returns the contained folders.
   * 
   * @return array of LocalFolder.
   * 
   * @throws PerforceException in case of an error.
   */
  public LocalFolder[] getFolders() throws PerforceException {
    try {
      IContainer container = (IContainer) toResource();
      IResource[] members = container.members();

      List<LocalFolder> result = Lists.newArrayList();
      for (int i = 0, length = members.length; i < length; i++) {
        if (members[i].getType() != IResource.FILE) {
          result.add(LocalFolder.create(getServer(), members[i]));
        }
      }

      return result.toArray(new LocalFolder[0]);
    } catch (CoreException e) {
      throw new PerforceException(e.getStatus());
    }
  }

  /**
   * Folder cannot be checked out.
   * 
   * @return false
   */
  @Override
  public boolean isCheckedOut() {
    return false;
  }

  @Override
  public boolean isResolved() {
    return true;
  }

  /**
   * Folder cannot be dirty.
   * 
   * @return false
   */
  @Override
  public boolean isDirty() {
    return false;
  }

  /**
   * Folder cannot be out of date.
   * 
   * @return false
   */
  @Override
  public boolean isOutOfDate() {
    return false;
  }

  /**
   * Returns the FolderSyncInfo of the container.
   * 
   * @return the FolderSyncInfo.
   */
  public FolderSyncInfo getFolderSync() {
    try {
      return PerforceSynchronizer.getInstance().getFolderSync((IContainer) toResource());
    } catch (PerforceException e) {
      PerforceCorePlugin.log(e.getStatus());
    }

    return null;
  }

  /**
   * Folders do not have ResourceSyncInfo.
   * 
   * @return null
   */
  @Override
  public ResourceSyncInfo getSyncInfo() {
    return null;
  }

  @Override
  public boolean isCheckedOutForAdd() {
    return false;
  }

  @Override
  public boolean isCheckedOutForDelete() {
    return false;
  }
}
